<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="出发地点" prop="startAddress">
        <el-input
          v-model="queryParams.startAddress"
          placeholder="请输入出发地点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到达地点" prop="endAddress">
        <el-input
          v-model="queryParams.endAddress"
          placeholder="请输入到达地点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物名称" prop="goodsInfo">
        <el-input
          v-model="queryParams.goodsInfo"
          placeholder="请输入货物名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运费" prop="transportCost">
        <el-input
          v-model="queryParams.transportCost"
          placeholder="请输入运费"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否推荐" prop="isRecommend">
        <el-select v-model="queryParams.isRecommend" placeholder="请选择是否推荐" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="装货时间" prop="loadTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.loadTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择装货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="车型" prop="carStyle">
        <el-input
          v-model="queryParams.carStyle"
          placeholder="请输入车型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车长" prop="carLength">
        <el-input
          v-model="queryParams.carLength"
          placeholder="请输入车长"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="载重" prop="loadCapacity">
        <el-input
          v-model="queryParams.loadCapacity"
          placeholder="请输入载重"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="contact">
        <el-input
          v-model="queryParams.contact"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车重" prop="carWeight">
        <el-input
          v-model="queryParams.carWeight"
          placeholder="请输入车重"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="车的体积" prop="carVolume">-->
<!--        <el-input-->
<!--          v-model="queryParams.carVolume"-->
<!--          placeholder="请输入车的体积"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="货物预计送达时间" prop="expectedTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.expectedTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择货物预计送达时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="货物体积" prop="goodsVolume">
        <el-input
          v-model="queryParams.goodsVolume"
          placeholder="请输入货物体积"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物重量" prop="goodsWeight">
        <el-input
          v-model="queryParams.goodsWeight"
          placeholder="请输入货物重量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.auth_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一标识数据表" align="center" prop="id" />
      <el-table-column label="出发地点" align="center" prop="startAddress" />
      <el-table-column label="到达地点" align="center" prop="endAddress" />
      <el-table-column label="货物名称" align="center" prop="goodsInfo" />
      <el-table-column label="运费" align="center" prop="transportCost" />
      <el-table-column label="是否推荐" align="center" prop="isRecommend">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.isRecommend"/>
        </template>
      </el-table-column>
      <el-table-column label="装货时间" align="center" prop="loadTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loadTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车型" align="center" prop="carStyle" />
      <el-table-column label="车长" align="center" prop="carLength" />
      <el-table-column label="载重" align="center" prop="loadCapacity" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="联系方式" align="center" prop="contact" />
      <el-table-column label="备注" align="center" prop="comment" />
      <el-table-column label="车重" align="center" prop="carWeight" />
<!--      <el-table-column label="车的体积" align="center" prop="carVolume" />-->
      <el-table-column label="货物预计送达时间" align="center" prop="expectedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="货物体积" align="center" prop="goodsVolume" />
      <el-table-column label="货物重量" align="center" prop="goodsWeight" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.auth_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改货源信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出发地点" prop="startAddress">
          <el-input v-model="form.startAddress" placeholder="请输入出发地点" />
        </el-form-item>
        <el-form-item label="到达地点" prop="endAddress">
          <el-input v-model="form.endAddress" placeholder="请输入到达地点" />
        </el-form-item>
        <el-form-item label="货物名称" prop="goodsInfo">
          <el-input v-model="form.goodsInfo" placeholder="请输入货物名称" />
        </el-form-item>
        <el-form-item label="运费" prop="transportCost">
          <el-input v-model="form.transportCost" placeholder="请输入运费" />
        </el-form-item>
        <el-form-item label="是否推荐">
          <el-radio-group v-model="form.isRecommend">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="装货时间" prop="loadTime">
          <el-date-picker clearable size="small"
            v-model="form.loadTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择装货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车型" prop="carStyle">
          <el-input v-model="form.carStyle" placeholder="请输入车型" />
        </el-form-item>
        <el-form-item label="车长" prop="carLength">
          <el-input v-model="form.carLength" placeholder="请输入车长" />
        </el-form-item>
        <el-form-item label="载重" prop="loadCapacity">
          <el-input v-model="form.loadCapacity" placeholder="请输入载重" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="备注" prop="comment">
          <el-input v-model="form.comment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="车重" prop="carWeight">
          <el-input v-model="form.carWeight" placeholder="请输入车重" />
        </el-form-item>
<!--        <el-form-item label="车的体积" prop="carVolume">-->
<!--          <el-input v-model="form.carVolume" placeholder="请输入车的体积" />-->
<!--        </el-form-item>-->
        <el-form-item label="货物预计送达时间" prop="expectedTime">
          <el-date-picker clearable size="small"
            v-model="form.expectedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择货物预计送达时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="货物体积" prop="goodsVolume">
          <el-input v-model="form.goodsVolume" placeholder="请输入货物体积" />
        </el-form-item>
        <el-form-item label="货物重量" prop="goodsWeight">
          <el-input v-model="form.goodsWeight" placeholder="请输入货物重量" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.auth_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/business/goods";

export default {
  name: "Goods",
  dicts: ['sys_normal_disable', 'auth_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货源信息表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startAddress: null,
        endAddress: null,
        goodsInfo: null,
        transportCost: null,
        isRecommend: null,
        loadTime: null,
        carStyle: null,
        carLength: null,
        loadCapacity: null,
        name: null,
        contact: null,
        comment: null,
        carWeight: null,
        carVolume: null,
        expectedTime: null,
        goodsVolumegoodsVolume: null,
        goodsWeight: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startAddress: [
          { required: true, message: "出发地点不能为空", trigger: "blur" }
        ],
        endAddress: [
          { required: true, message: "到达地点不能为空", trigger: "blur" }
        ],
        goodsInfo: [
          { required: true, message: "货物名称不能为空", trigger: "blur" }
        ],
        transportCost: [
          { required: true, message: "运费不能为空", trigger: "blur" }
        ],
        isRecommend: [
          { required: true, message: "是否推荐不能为空", trigger: "blur" }
        ],
        loadTime: [
          { required: true, message: "装货时间不能为空", trigger: "blur" }
        ],
        carStyle: [
          { required: true, message: "车型不能为空", trigger: "blur" }
        ],
        carLength: [
          { required: true, message: "车长不能为空", trigger: "blur" }
        ],
        loadCapacity: [
          { required: true, message: "载重不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        contact: [
          { required: true, message: "联系方式不能为空", trigger: "blur" }
        ],
        comment: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        carWeight: [
          { required: true, message: "车重不能为空", trigger: "blur" }
        ],
        // carVolume: [
        //   { required: true, message: "车的体积不能为空", trigger: "blur" }
        // ],
        expectedTime: [
          { required: true, message: "货物预计送达时间不能为空", trigger: "blur" }
        ],
        goodsVolume: [
          { required: true, message: "货物体积不能为空", trigger: "blur" }
        ],
        goodsWeight: [
          { required: true, message: "货物重量不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询货源信息列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        startAddress: null,
        endAddress: null,
        goodsInfo: null,
        transportCost: null,
        isRecommend: "0",
        loadTime: null,
        carStyle: null,
        carLength: null,
        loadCapacity: null,
        name: null,
        contact: null,
        comment: null,
        carWeight: null,
        carVolume: null,
        expectedTime: null,
        goodsVolume: null,
        goodsWeight: null,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加货源信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改货源信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除货源信息编号为"' + ids + '"的数据项？').then(function() {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
